/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import entity.ExpectedYearOnYearGrowthEntity;
import entity.MarketSalesReportEntity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;
import type.MarketSalesType;
import type.Month;
import type.SalesForecastPeriodType;

/**
 *
 * @author wei
 */
@Stateless
public class ExpectedYearOnYearGrowthEntityFacade extends AbstractFacade<ExpectedYearOnYearGrowthEntity> {
    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public ExpectedYearOnYearGrowthEntityFacade() {
        super(ExpectedYearOnYearGrowthEntity.class);
    }

    public boolean checkExistence(SalesForecastPeriodType period, int year) {
//        System.out.println("the period is "+ period);
//        System.out.println("the year is "+ year);
        Query query = (Query) em.createQuery("Select r from ExpectedYearOnYearGrowth r WHERE r.yearOfSales=:yearOfProduction");
        query.setParameter("yearOfProduction", year);
//        query.setParameter("monthOfProduction",period);
      
        List<ExpectedYearOnYearGrowthEntity> mrpList = new ArrayList<ExpectedYearOnYearGrowthEntity>();
        List<ExpectedYearOnYearGrowthEntity> mrpList2 = new ArrayList<ExpectedYearOnYearGrowthEntity>();
        try {
          mrpList = (List<ExpectedYearOnYearGrowthEntity>) query.getResultList();
          for(ExpectedYearOnYearGrowthEntity k:mrpList){
                 if(!k.isArchived()){
                  mrpList2.add(k);
             }
          
//           for(RawMaterialRequirementWeeklyEntity r:list){
//               if (r.getRawMaterial().getProductName().equals(material.getProductName())){
//                   mrpList.add(r);
//                   
//               }
           }
        }
//            System.out.println("List.size is "+mpsList.size());
         catch (NoResultException e) {
            System.out.println("no result found");
        } 
        if(mrpList2.isEmpty()){
//            System.out.println("lala");
            return false;
        }else{
            for(ExpectedYearOnYearGrowthEntity e:mrpList2){
                System.out.println(e.getForecastPeriodType());
                System.out.println(period);
                if(e.getForecastPeriodType()==period){
                    return true;
                }
            }
            return false;
        }
    }

    public ArrayList<MarketSalesReportEntity> findMaketSalesReport(MarketSalesType type,int year) {
        ArrayList<MarketSalesReportEntity> salesReports = new ArrayList<MarketSalesReportEntity>();
        for ( int i = year-5; i<= year-1; i++){
            System.out.println("finding year "+ i);
         Query query = (Query) em.createQuery("select s from MarketSalesReport s where s.year=:year and s.salesType=:type");
        query.setParameter("year", i);
        query.setParameter("type",type);
       
//        System.out.println("1. the size is "+ query.getResultList().size());
        MarketSalesReportEntity report= (MarketSalesReportEntity) query.getSingleResult(); 
//        System.out.println(reports.size());
//        for(MarketSalesReportEntity r: reports){
//            if(r.getSalesType()==type)
        
        salesReports.add(report);    
        
       }
        return salesReports;
    }
    
    public List<ExpectedYearOnYearGrowthEntity> findAll() {
        CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
        javax.persistence.criteria.CriteriaQuery cq = builder.createQuery();
        Root<ExpectedYearOnYearGrowthEntity> dataObjectRoot = cq.from(ExpectedYearOnYearGrowthEntity.class);
        cq.select(dataObjectRoot);
        cq.where(builder.and(builder.equal(dataObjectRoot.get("archived"), false), builder.equal(dataObjectRoot.get("generated"), false)));
        return getEntityManager().createQuery(cq).getResultList();
    }
    
}
